﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SYH.Config;
using System.Data.SqlClient;

namespace SYH.Product.DAL
{
   public  class Industry
    {
        /// <summary>
        /// 根据ParentId获取列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetIndustriesList(int parentId)
        {
            return Common.SqlHelper.ExecuteDataset(
                WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "Public_Industries_GetList",
                new SqlParameter("@parentId", parentId)
                ).Tables[0];
        }

        /// <summary>
        /// 得到用户产品三级行业名称
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="top">记录数</param>
        /// <returns></returns>
        public DataSet GetUserIndustries3Name(int userid, int top)
        {
            //StringBuilder strsql = new StringBuilder();
            //strsql.Append(" select ");
            //if (top > 0)
            //{
            //    strsql.AppendFormat(" top {0} ", top);
            //}
            //strsql.Append(" b.Id,b.ChsName from Public_Industries b  where exists (");
            //strsql.AppendFormat(" select  DISTINCT IndustryId from Public_IndustryData a where a.datauserid={0} and a.industrylevel=3 and a.DataType=0 and b.Id=a.IndustryId)", userid);

            string sql = string.Format(@" with t as (
  select top {1} IndustryId3 as IndustryIdLv3 from product_info where userid={0} group by IndustryId3
  )
  select i.Id,ChsName from Public_Industries i inner join t on i.Id=t.IndustryIdLv3", userid, top);
            return Common.SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.Text, sql.ToString());

        }
    }
}
